﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class BoxManager
{
    public static GameObject m_kBox;
    public static GameObject netBox;
    public static GameObject netMask;
    public static GameObject socketBox;
    public static GameObject levelUpPage;   //玩家升级界面
    private static bool easyTouchActive = false;

    private static List<GameObject> m_kOpenBox = new List<GameObject>();


    public static GameObject m_levelupBox;
    private static Transform anthor;
    public static Transform Anthor
    {
        get
        {
            if (anthor == null)
            {
                anthor = GameObject.FindGameObjectWithTag("uiroot").transform;
            }
            return anthor;
        }
    }
    public static void CleanAllBox()
    {
        if (m_kBox != null)
        {
            GameObject.Destroy(m_kBox);
        }
        //if (lsPopupTweenBox != null && lsPopupTweenBox.Count != 0)
        //{
        //    for (int i = 0; i < lsPopupTweenBox.Count; i++)
        //    {
        //        GameObject _go = lsPopupTweenBox[i];
        //        if (_go != null)
        //        {
        //            UIPanel _pa = _go.GetComponent<UIPanel>();
        //            if (_pa.alpha <= 0)
        //            {
        //                GameObject.Destroy(_go);
        //                lsPopupTweenBox.RemoveAt(i--);
        //            }
        //        }
        //    }
        //}
        if (m_kOpenBox != null && m_kOpenBox.Count > 0)
        {
            for (int i = 0; i < m_kOpenBox.Count; i++)
            {
                GameObject _go = m_kOpenBox[i];
                if (_go != null)
                {
                    GameObject.Destroy(_go);
                }
                m_kOpenBox.RemoveAt(i--);
            }
        }
    }

    public static void ClearNetMask()
    {
        if (netMask != null)
        {
            if (netMask.activeSelf)
            {
                if (EasyTouchTool.Instance != null && easyTouchActive)
                    EasyTouchTool.Instance.ShowJoystick();
            }
            netMask.SetActive(false);
        }
        if (netBox != null)
        {
            GameObject.Destroy(netBox);
            netBox = null;
        }
    }

    public static void ClearSocketBox()
    {
        if (socketBox != null)
        {
            GameObject.Destroy(socketBox);
            socketBox = null;
        }
    }

    public static void CreateNetMask()
    {
        ClearNetMask();
        if (netMask == null)
        {
            GameObject mask = GameObject.Instantiate(Resources.Load("UI/MessageBox/NetMask")) as GameObject;
            mask.transform.parent = Anthor;
            mask.transform.localScale = Vector3.one;
            mask.transform.localPosition = new Vector3(0, 0, -500);
            netMask = mask;
        }

        if (EasyTouchTool.Instance != null)
        {
            easyTouchActive = EasyTouchTool.Instance.Enable;
            if (easyTouchActive)
                EasyTouchTool.Instance.Hide();
        }

        netMask.SetActive(true);
    }

    public static void CreateNetError(string sError)
    {
        CreatePopupTextBox(sError);
    }

    public static void CreateNetTimeOut(System.Action click)
    {
        ClearNetMask();
        GameObject box = GameObject.Instantiate(Resources.Load("UI/MessageBox/NetError")) as GameObject;
        box.transform.parent = Anthor;
        box.transform.localScale = Vector3.one;
        box.transform.localPosition = Vector3.zero;
        box.GetComponent<NetErrorWindow>().ShowNetError(1, click);
        netBox = box;
    }

    public static void CreateSocketReconnect(string msg, System.Action click)
    {
        if (forceNeedRelogin)//如果需要重新登录
            return;

        ClearSocketBox();
        if ( (StaticData.curBattleData.battleType == BattleType.FIELD_PVE || 
            StaticData.curBattleData.battleType == BattleType.FIELD_BOSS) && 
            Room.RoomManager.curSceneType == SceneType.BATTLE)
        {
            GameObject box = GameObject.Instantiate(Resources.Load("UI/MessageBox/NetErrorBackScene")) as GameObject;
            box.transform.parent = Anthor;
            box.transform.localScale = Vector3.one;
            box.transform.localPosition = Vector3.zero;
            StaticData.netBreak = true;
            //box.GetComponent<NetErrorBackScene>().ShowNetError(click);
            socketBox = box;
        }
        else
        {
            
            GameObject box = GameObject.Instantiate(Resources.Load("UI/MessageBox/NetError")) as GameObject;
            box.transform.parent = Anthor;
            box.transform.localScale = Vector3.one;
            box.transform.localPosition = Vector3.zero;

            box.GetComponent<NetErrorWindow>().ShowNetError(2, click);
            socketBox = box;
        }
    }

    public static bool forceNeedRelogin = false;
    public static void CreateReloginBox(string msg)
    {
        ClearSocketBox();
        forceNeedRelogin = true;
        GameObject box = GameObject.Instantiate(Resources.Load("UI/MessageBox/NetError")) as GameObject;
        box.transform.parent = Anthor;
        box.transform.localScale = Vector3.one;
        box.transform.localPosition = Vector3.zero;

        box.GetComponent<NetErrorWindow>().ShowNetError(2, null);
        box.GetComponent<NetErrorWindow>().reconnectBtn.GetComponent<BoxCollider>().gameObject.SetActive(false);
        var tran = box.GetComponent<NetErrorWindow>().reloginBtn.transform.localPosition;
        box.GetComponent<NetErrorWindow>().reloginBtn.transform.localPosition = new Vector3(0, tran.y, 0);
        if (!string.IsNullOrEmpty(msg))
        {
            box.GetComponent<NetErrorWindow>().text.text = msg;
        }
        socketBox = box;
        
    }


    public static void CreateMsgBox(string sValue, bool bBtnResendMsg)
    {
        CleanAllBox();
        GameObject box = GameObject.Instantiate(Resources.Load("UI/MessageBox/MessageBox")) as GameObject;
        box.transform.parent = Anthor;
        box.transform.localScale = Vector3.one;
        box.transform.localPosition = Vector3.zero;
        MessageBoxController mbc = box.GetComponent<MessageBoxController>();
        mbc.Init(sValue, bBtnResendMsg);

        m_kBox = box;
    }

    public static void CreateMsgBox(string sValue)
    {
        CreateMsgBox(sValue, false);
    }

    public static void CreateMsgBox_Cloth(string sValue)
    {
        CleanAllBox();
        GameObject box = GameObject.Instantiate(Resources.Load("UI/MessageBox/MessageBoxCloth")) as GameObject;
        box.transform.parent = Anthor;
        box.transform.localScale = Vector3.one;
        box.transform.localPosition = Vector3.zero;
        MessageBoxController mbc = box.GetComponent<MessageBoxController>();
        mbc.Init(sValue, false);

        m_kBox = box;
    }

    public static void CreateOneDelMsgBox(string txt, MessageBoxDelegateCtrl.ClickListener click)
    {
        GameObject box = GameObject.Instantiate(Resources.Load("UI/MessageBox/OneDelMsgBox")) as GameObject;
        box.transform.parent = Anthor;
        box.transform.localScale = Vector3.one;
        box.transform.localPosition = Vector3.zero;

        box.GetComponent<MessageBoxDelegateCtrl>().Init(txt, click);
        m_kBox = box;
    }

    //都换成由下往上的接口
    public static void CreatePopupTextBox(string txt,float delay=0f)
    {
        if (delay == 0f)
        {
            CleanAllBox();
            GameObject box = GameObject.Instantiate(Resources.Load("UI/MessageBox/PopupTweenTextBox")) as GameObject;
            box.transform.parent = Anthor;
            box.transform.localScale = Vector3.one;
            box.transform.localPosition = Vector3.zero;

            box.GetComponent<PopupTweenTextBox>().Init(txt);
        }
        else
        {
            UI.PageManager.Instance.StartCoroutine(_CreatePopupTextBox(txt, delay));
        }
    }

    private static IEnumerator _CreatePopupTextBox(string txt,float delay)
    {
        yield return new WaitForSeconds(delay);
        CleanAllBox();
        GameObject box = GameObject.Instantiate(Resources.Load("UI/MessageBox/PopupTweenTextBox")) as GameObject;
        box.transform.parent = Anthor;
        box.transform.localScale = Vector3.one;
        box.transform.localPosition = Vector3.zero;

        box.GetComponent<PopupTweenTextBox>().Init(txt);
    }

    //private static List<GameObject> lsPopupTweenBox = new List<GameObject>();

    public static void CreateOrderSuccess(string txt)
    {
        GameObject box = GameObject.Instantiate(Resources.Load("UI/MessageBox/PopupTweenTextBox")) as GameObject;
        box.transform.parent = Anthor;
        box.transform.localScale = Vector3.one;
        box.transform.localPosition = Vector3.zero;

        box.GetComponent<PopupTweenTextBox>().Init(txt);
    }

    public delegate void MessageBoxDelegate(bool a);

    public static void CreateMessageBoxWithCofirmAndCancelButton(string context, bool isConfirmBtnLeft, MessageBoxDelegate a)
    {
        CleanAllBox();
        GameObject box = GameObject.Instantiate(Resources.Load("UI/MessageBox/MessageBoxWithConfirmAndCancelButton")) as GameObject;
        box.transform.parent = Anthor;
        box.transform.localPosition = Vector3.zero;
        box.transform.localScale = Vector3.one;

        box.GetComponent<MessageBoxWidth2BtnController>().Init(context, isConfirmBtnLeft, a);
        m_kBox = box;
    }

    public static void CreateMessageBoxWithCofirmAndCancelButton(string context, bool isConfirmBtnLeft, MessageBoxDelegate a, string title)
    {
        CleanAllBox();
        GameObject box = GameObject.Instantiate(Resources.Load("UI/MessageBox/MessageBoxWithConfirmAndCancelButton")) as GameObject;
        box.transform.parent = Anthor;
        box.transform.localPosition = Vector3.zero;
        box.transform.localScale = Vector3.one;

        box.GetComponent<MessageBoxWidth2BtnController>().Init(context, isConfirmBtnLeft, a, title);
        m_kBox = box;
    }

    public static void CreateMessageBoxWithCofirmAndCancelButton(string context, MessageBoxDelegate a)
    {
        CleanAllBox();
        GameObject box = GameObject.Instantiate(Resources.Load("UI/MessageBox/MessageBoxWithConfirmAndCancelButton")) as GameObject;
        box.transform.parent = Anthor;
        box.transform.localPosition = Vector3.zero;
        box.transform.localScale = Vector3.one;

        box.GetComponent<MessageBoxWidth2BtnController>().Init(context, a);
        m_kBox = box;
    }
    public static void CreateMessageBoxWithoutCancelButton(string context,bool isConfirmBtnLeft,MessageBoxDelegate a,string title,bool hasCancle)
    {
        CleanAllBox();
        GameObject box = GameObject.Instantiate(Resources.Load("UI/MessageBox/MessageBoxWithConfirmAndCancelButton")) as GameObject;
        box.transform.parent = Anthor;
        box.transform.localPosition = Vector3.zero;
        box.transform.localScale = Vector3.one;
        box.GetComponent<MessageBoxWidth2BtnController>().Init(context, isConfirmBtnLeft, a, title, hasCancle);
        m_kBox = box;
    }

    /// <summary>
    /// 遇到悬赏任务BOSS时的弹窗
    /// </summary>
    /// 
    public static void CreateMeetBossBox(string wantedId)
    {
        Dict.DataModel.TableWantedLevel wantedLvData = Dict.Blo.DictWantedBlo.GetTableWantedLevelData(wantedId);
        string heroId = Dict.Blo.DictWantedBlo.GetWantedLeaderId(wantedId);

        CleanAllBox();
        GameObject box = GameObject.Instantiate(Resources.Load("UI/MessageBox/CopyMeetBossBox")) as GameObject;
        box.transform.parent = Anthor;
        box.transform.localPosition = Vector3.zero;
        box.transform.localScale = Vector3.one;

        box.GetComponent<CopyMeetBossBox>().Init(heroId, wantedLvData.Level);

        m_kBox = box;
    }
    
    public static void CreateCountdownBoxTwoButton(string content,bool isComfirmLeft,float timer = 10f, BoxManager.MessageBoxDelegate ok =null,string title = "")
    {
        CleanAllBox();

        GameObject box = GameObject.Instantiate(Resources.Load("UI/MessageBox/MessageBoxCountdown")) as GameObject;
        box.transform.parent = Anthor;
        box.transform.localPosition = Vector3.zero;
        box.transform.localScale = Vector3.one;

        box.GetComponent<MessageBoxCountdown>().Init(content, isComfirmLeft, ok,timer);
        m_kBox = box;
    }


    public static void CreateCountdownBoxOneButton(string content, float timer = 10f, string title = "")
    {
        CleanAllBox();

        GameObject box = GameObject.Instantiate(Resources.Load("UI/MessageBox/MessageBoxCountdown")) as GameObject;
        box.transform.parent = Anthor;
        box.transform.localPosition = Vector3.zero;
        box.transform.localScale = Vector3.one;

        box.GetComponent<MessageBoxCountdown>().Init(content, false, null
            , title, false, timer);
        m_kBox = box;
    }

    public static void CreateMarqueeBox(string str)
    {
        var scroll = GameObject.Instantiate(Resources.Load<GameObject>("prefab/Marquee"));
        scroll.transform.SetParent(UICamera.mainCamera.gameObject.transform);
        scroll.transform.localScale = Vector3.one;
        scroll.transform.localPosition = Vector3.zero;
        scroll.transform.SetParent(UICamera.mainCamera.gameObject.transform.parent);
        scroll.SetActive(true);

        var script = scroll.GetComponent<Marquee>();
        script.Init(str);
    }

    public static void ShowHeroFull()
    {
        GameObject box = GameObject.Instantiate(Resources.Load("UI/MessageBox/HeroFull")) as GameObject;
        box.transform.parent = Anthor;
        box.transform.localPosition = Vector3.zero;
        box.transform.localScale = Vector3.one;
    }

    public static void ShowEquipFull()
    {
        GameObject box = GameObject.Instantiate(Resources.Load("UI/MessageBox/EquipFull")) as GameObject;
        box.transform.parent = Anthor;
        box.transform.localPosition = Vector3.zero;
        box.transform.localScale = Vector3.one;
    }

    public static void CreateLevelUp(int from,int to)
    {
        GameObject.Destroy(levelUpPage);
        levelUpPage = GameObject.Instantiate(Resources.Load("UI/MessageBox/PlayerLevelUp")) as GameObject;
        levelUpPage.GetComponent<PlayerLevelUp>().SetLevelUp(from, to);
        levelUpPage.transform.parent = Anthor;
        levelUpPage.transform.localPosition = Vector3.zero;
        levelUpPage.transform.localScale = Vector3.one;
    }

    //public static GameObject CreateRedPoint(Transform parent,Vector3 offset)//父节点和右上角偏移
    //{
    //    GameObject redPoint = GameObject.Instantiate(Resources.Load("UI/MessageBox/RedPointTip")) as GameObject;
    //    BoxCollider boxCol = parent.GetComponent<BoxCollider>();
    //    if (boxCol != null)
    //    {
    //        redPoint.transform.parent = parent;
    //        redPoint.transform.localPosition = boxCol.center + boxCol.size / 2 + offset;
    //        redPoint.transform.localScale = Vector3.one;
    //    }
    //    return redPoint;
    //}
}
